﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace DYSales.Views.Stock
{
    public partial class mgstock_page : Page
    {
        public mgstock_page()
        {
            InitializeComponent();
            dg_sts.AutoGeneratingColumn += other.GetOther().dg_AutoGeneratingColumn;
            Loaded += new RoutedEventHandler(mgstock_page_Loaded);
            bt_删除.Click += new RoutedEventHandler(bt_删除_Click);
        }

        void bt_删除_Click(object sender, RoutedEventArgs e)
        {
            if (dg_sts.Selection.SelectedRows.Count >= 1)
            {
                var box = other.GetMessageBox();
                box.Title = "东雅进销存提示";
                box.tbk_text.Text = "确定要删除选中的记录吗?";
                box.Show();
                box.Closed += new EventHandler(box_Closed);
            }
        }

        void box_Closed(object sender, EventArgs e)
        {
            var cwin = sender as cwin_Question;
            cwin.Closed -= new EventHandler(box_Closed);
            if (cwin.DialogResult.Value)
            {
                dm.getDS().Load(dm.getDS().GetPdTablesQuery(), lp =>
                {
                    foreach (var item in dg_sts.Selection.SelectedRows)
                    {
                        var sa = item.DataItem as Web.StockAdd;
                        if (sa != null)
                        {
                            var pd = lp.Entities.FirstOrDefault(p => p.p_商品名称.Equals(sa.st_商品名称));
                            if (pd != null)
                            {
                                if (pd.p_库存数量 >= sa.st_进货数量)
                                {
                                    if (pd.p_进货价 == sa.st_进货价)
                                    {
                                        pd.p_库存数量 -= sa.st_进货数量;
                                    }
                                    else
                                    {
                                        decimal ototal = pd.p_库存数量 * pd.p_进货价;
                                        decimal ntotal = sa.st_进货价 * sa.st_进货数量;
                                        pd.p_库存数量 -= sa.st_进货数量;
                                        pd.p_进货价 = (ototal - ntotal) / pd.p_库存数量;
                                    }
                                }
                                else
                                {
                                    var box = other.GetMessageBox();
                                    box.Title = "东雅进销存提示";
                                    box.tbk_text.Text = "应该批号的商品库存量已经少于进货数量，因此不能删除此批号进货记录。";
                                    box.Show();
                                    return;
                                }
                            }
                            dm.getDS().StockAdds.Remove(sa);
                        }
                    }
                    dm.getDS().SubmitChanges(lo => { refresh(); }, null);
                }, null);
            }
        }

        void mgstock_page_Loaded(object sender, RoutedEventArgs e)
        {
            refresh();
        }

        void refresh()
        {
            dm.getDS().Load(dm.getDS().GetStockAddsQuery(), lo => {
                dg_sts.DataContext = lo.Entities;
            }, null);
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

    }
}
